// pages/login/login.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    telphone: '',
    password: '',
    imgSrc: '',
    headImage: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      telphone: options.Tel
    })
  },
  //用户登录
  login() {
    if (!this.data.telphone == '' && !this.data.password == '') {
      //获取账号信息
      let that = this
      let db = wx.cloud.database()
      db.collection('CR_user').where({
        telphone: this.data.telphone
      }).get({
        success: (res) => {
          if (res.data.length > 0) {
            //  登陆成功
            if (that.data.password == res.data[0].password) {
              wx.setStorageSync('Tel', this.data.telphone)
              wx.switchTab({
                url: '/pages/home/home'
              })
            } else {
              wx.showToast({
                title: '账号或密码错误',
                icon: 'none',
                success: (res) => {},
                fail: (res) => {},
                complete: (res) => {},
              })
            }
          } else {
            wx.showToast({
              title: '账号或密码错误',
              icon: 'none',
              success: (res) => {},
              fail: (res) => {},
              complete: (res) => {},
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '账号或密码不能为空',
        icon: 'none',
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }
  },

  //注册账号跳转
  register() {
    wx.navigateTo({
      url: '/pages/register/register'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  gettelphone(e) {
    this.setData({
      telphone: e.detail.value
    })
    let db = wx.cloud.database()
    db.collection('CR_user').where({
      telphone: this.data.telphone
    }).get({
      success: (res) => {
        if (res.data.length >= 1) {
          this.setData({
            headImage: true,
            imgSrc: res.data[0].headImage
          })
        } else {
          this.setData({
            headImage: false,
            imgSrc:''
          })
        }
      }
    })
  },
  getpassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})